package com.hzncc.flowable_diboot.config;

import com.diboot.core.cache.DynamicMemoryCacheManager;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

/**
 * Spring 相关配置
 *
 * @author www.dibo.ltd
 * @version v1.0
 * @date 2020/10/23
 */
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.hzncc"})
@MapperScan(basePackages = {"com.hzncc.flowable_diboot.mapper","com.hzncc.flowable_diboot.project_contracts.mapper",
        "com.hzncc.flowable_diboot.custom_field.mapper","com.hzncc.flowable_diboot.trial_contracts.mapper"})
@EnableCaching
public class SpringMvcConfig {

    @Bean
    @Primary
    public CacheManager cacheManager() {
        return new DynamicMemoryCacheManager(ConstantUtil.CACHE_CITY,ConstantUtil.CACHE_AREA,ConstantUtil.CACHE_USER_ROLE,ConstantUtil.CACHE_ROLE_TENANT);
    }

    @Bean
    public FilterRegistrationBean<MethodChangeFilter> myFilter() {
        FilterRegistrationBean<MethodChangeFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MethodChangeFilter());
        registrationBean.addUrlPatterns("/*"); // 设置过滤器应用的URL模式
        registrationBean.setOrder(1); // 设置过滤器顺序
        return registrationBean;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(30*1000);//单位为ms
        factory.setConnectTimeout(60*1000);//单位为ms
        return factory;
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate=  new RestTemplate(factory);
        //restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
}